package frames.kits;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.awt.*;
import java.util.*;
import java.util.List;

@Component
public class Datas {
    private Integer[][] mat;
    private Random random;
    private Set<Pointing> booms;
    @Autowired
    private TextConsole textConsole;
    @Value("${game.row}")
    private int row;
    @Value("${game.col}")
    private int col;
    @PostConstruct
    public void init(){
        mat=new Integer[row][col];
        random = new Random();
        booms = new HashSet<>();
    }
    public void refresh(int k){
        int x;
        int y;
        while (true){
            if (booms.size()>=k)break;

            x=random.nextInt(row);
            y=random.nextInt(col);
//            if (booms.contains xy) continue
            booms.add(new Pointing(x,y));
        }
//        给雷区赋值
        textConsole.debug(booms);
        for (Pointing boom : booms) {
            mat[boom.getX()][boom.getY()]=-1;
        }
        int c = 0;
        for (Integer[] row : mat) {
            for (Integer cell : row) {
                System.out.print(cell+" | ");
                if (cell !=null){
                    c++;
                }
            }
            System.out.println();
        }
        System.out.println("总共埋雷"+c);
//        给非雷区赋值
        for (int i = 0; i < mat.length; i++) {
            for (int j = 0; j < mat[i].length; j++) {
                if (mat[i][j] == null){
                    int count =0;
                    for (int m = -1; m <=1 ; m++) {
                        for (int n = -1; n <=1 ; n++) {
                            try{
                                if (mat[i+m][j+n] ==-1){
                                    count++;
                                }
                            }catch (Exception e){
                                continue;
                            }

                        }
                    }
                    mat[i][j]=count;
                }
            }
        }
        for (Integer[] row : mat) {
            for (Integer cell : row) {
                System.out.print(cell+" | ");
            }
            System.out.println();
        }
    }

    public Integer[][] getMat() {
        return mat;
    }

    public Random getRandom() {
        return random;
    }

    public Set<Pointing> getBooms() {
        return booms;
    }

    public int getRow() {
        return row;
    }

    public int getCol() {
        return col;
    }
}
class Pointing{

    private int x;
    private int y;

    public Pointing(int x, int y) {

        this.x = x;
        this.y = y;
    }

    public int getX() {
        return x;
    }

    public void setX(int x) {
        this.x = x;
    }

    public int getY() {
        return y;
    }

    public void setY(int y) {
        this.y = y;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Pointing pointing = (Pointing) o;
        return x == pointing.x &&
                y == pointing.y;
    }

    @Override
    public int hashCode() {
        return Objects.hash(x, y);
    }

    @Override
    public String toString() {
        return "Pointing{" +
                "x=" + x +
                ", y=" + y +
                '}';
    }
}